package net.lzzy.service;

import net.lzzy.entity.Article;
import net.lzzy.entity.Comment;
import net.lzzy.repository.ArticleRepository;
import net.lzzy.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private ArticleRepository articleRepository;

    public List<Comment> findCommentByArticleId(Integer id){
        return commentRepository.findByArticleId(id);
    }

    public void addComment(Integer id, String author, String content){
        Comment comment = new Comment();
        Article article = articleRepository.findById(id).get();
        comment.setArticle(article);
        comment.setAuthor(author);

        comment.setContent(content);
        comment.setDate(new Date());
        commentRepository.save(comment);
    }
}
